﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using BL.Abstract;
using BL.Class;
using BL.Generic;
using DAL.Abstract;
using DAL.Repositories;
using InventizerWEB.GenericObjects;
using InventizerWEB.Model;
using Model;
using PagedList;
using WebGrease.Css.Extensions;

namespace InventizerWEB.Controllers
{
    public class InventoryController : Controller
    {
        private readonly IInventoryBL inventoryBl;
        private readonly ISecurityHelper securityHelper;

        public InventoryController(ISecurityHelper securityHelper, IInventoryBL inventoryBl)
        {
            this.securityHelper = securityHelper;
            this.inventoryBl = inventoryBl;
        }

        public InventoryController()
        {
            securityHelper = new SecurityHelper();
            inventoryBl = new InventoryBL();
        }

        [Authorize]
        public ActionResult Index(int? page)
        {
            try
            {
                if (!securityHelper.CheckIfUserIsAdmin(User.Identity.Name))
                {
                    return RedirectToAction("Fobridden", "Error");
                }
                var pageNumber = (page ?? 1);
                var res = inventoryBl.GetAllInventories();
                if (res.BlAnswer == Answer.Success)
                {
                    return View(res.ObjectAnswer.OrderBy(i => i.INVENTORYID)
                        .ToPagedList(pageNumber, ApplicationSettings.pageSize));
                }
                return RedirectToAction("Error", "Error");
            }
            catch (Exception ex)
            {
                TempData["Error"] = "Wystąpił błąd: " + ex.Message;
                return RedirectToAction("Error", "Error");
            }
        }

        [Authorize]
        public ActionResult Default()
        {
            try
            {
                var appSettings = new ApplicationSettingsVM()
                {
                    Inventories = new List<SelectListItem>(),
                    SelectedInventoryId = "0"
                };
                inventoryBl.GetAllInventories().ObjectAnswer.ForEach(item =>
                {
                    appSettings.Inventories.Add(new SelectListItem
                    {
                        Text = item.NAME,
                        Value = item.INVENTORYID.ToString()
                    });
                });
                return View(appSettings);
            }
            catch (Exception ex)
            {
                TempData["Error"] = "Wystąpił błąd: " + ex.Message;
                return RedirectToAction("Error", "Error");
            }
        }

        [Authorize]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Default(ApplicationSettingsVM appSettingsVm)
        {
            try
            {
                if (appSettingsVm.SelectedInventoryId == null)
                {
                    return RedirectToAction("Default");
                }
                ApplicationSettings.InventoryId = appSettingsVm.SelectedInventoryId;
                return RedirectToAction("Index", "Home");
            }
            catch (Exception ex)
            {
                TempData["Error"] = "Wystąpił błąd: " + ex.Message;
                return RedirectToAction("Error", "Error");
            }
        }

        [Authorize]
        public ActionResult Create()
        {
            return User.Identity.Name != null && !securityHelper.CheckIfUserIsAdmin(User.Identity.Name)
                ? (ActionResult)RedirectToAction("Fobridden", "Error")
                : View();
        }

        [HttpGet]
        [Authorize]
        public ActionResult Edit(int id)
        {
            try
            {
                if (User.Identity.Name != null && !securityHelper.CheckIfUserIsAdmin(User.Identity.Name))
                {
                    return RedirectToAction("Fobridden", "Error");
                }
                var blAnswer = inventoryBl.GetInventoryById(id);
                if (blAnswer.BlAnswer == Answer.Error)
                {
                    TempData["Error"] = blAnswer.Exception;
                    return RedirectToAction("Error", "Error");
                }
                if (blAnswer.SingleObjectAnswer == null)
                {
                    TempData["Error"] = "W systemie brak inwentaryzacji o podanym Id.";
                    return RedirectToAction("Error", "Error");
                }
                return View("ExecuteEdit", blAnswer.SingleObjectAnswer);
            }
            catch (Exception ex)
            {
                TempData["Error"] = ex.Message;
                return RedirectToAction("Error", "Error");
            }
        }

        [Authorize]
        [HttpPost]
        public ActionResult ExecuteEdit(INVENTORY inventory)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (User.Identity.Name != null && !securityHelper.CheckIfUserIsAdmin(User.Identity.Name))
                    {
                        return RedirectToAction("Fobridden", "Error");
                    }
                    if (inventoryBl.UpdateInventory(inventory).BlAnswer == Answer.Success)
                    {
                        return RedirectToAction("Index");
                    }
                    return RedirectToAction("Error", "Error");
                }
                return RedirectToAction("Error", "Error");
            }
            catch (Exception ex)
            {
                return RedirectToAction("Error", "Error");
            }
        }

        [HttpPost]
        [Authorize]
        [ValidateAntiForgeryToken]
        public ActionResult Create(INVENTORY inventory)
        {
            try
            {
                if (User.Identity.Name != null && !securityHelper.CheckIfUserIsAdmin(User.Identity.Name))
                {
                    return RedirectToAction("Fobridden", "Error");
                }
                inventory.ISACTIVE = true;
                inventoryBl.SaveNewInventory(inventory);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return RedirectToAction("Error", "Error");
            }
        }
    }
}